<?php


namespace console\spider;


use backend\models\Major;
use backend\models\School;
use common\models\KMajorCatalog;
use console\spider\utils\FileUtils;
use console\spider\utils\HttpClient;
use console\spider\utils\TimeUtils;

class MajorSpider
{

    /**
     * 爬取专业信息
     * @return void
     */


    public static function getMajor()
    {
        //正常专业
        if(!file_exists(Params::majorZCDir)){//检查文件夹是否存在
            mkdir (Params::majorZCDir);    //没有就创建一个新文件夹
        }

        $params = Array();
        $params['dir'] = Params::majorZCDir;
        $url = Params::majorUrl.'&pqb.queryBean.zybxzt=1&pqb.pagingInfo.pageNum=';
        $params['fileName'] ='major';
        $params['tag']= "正常专业：";
        HttpClient::dataSave($url,$params);

        //停招专业
        if(!file_exists(Params::majorTZDir)){//检查文件夹是否存在
            mkdir (Params::majorTZDir);    //没有就创建一个新文件夹
        }

        $params = Array();
        $params['dir'] = Params::majorTZDir;
        $url = Params::majorUrl.'&pqb.queryBean.zybxzt=2&pqb.pagingInfo.pageNum=';
        $params['fileName'] ='major';
        $params['tag']= "停招专业：";
        HttpClient::dataSave($url,$params);
    }

    /**
     * 获取审核专业
     * @param $school
     */
    public static function getSHMajor($school)
    {

        if(!file_exists(Params::majorSHDir)){//检查文件夹是否存在
            mkdir (Params::majorSHDir);    //没有就创建一个新文件夹
        }

        $params = Array();
        $params['dir'] = Params::majorSHDir;
        $url = Params::majorShUrl."&nd=".TimeUtils::gettimestamp()."&zzXxXxzyywbQB.zzXxJbxxId=$school->id&pqb.pagingInfo.pageNum=";
        $params['fileName'] = $school->xxjgmc;
        $params['tag']= $school->xxjgmc."待审核专业：";
        HttpClient::dataSave($url,$params);
    }

    public static function analysisMajorData($tag)
    {
        $dir = __DIR__ . "\data\major-$tag\\";
        $files = FileUtils::findFileName($dir);
        foreach ($files as  $fileName) {
            echo "\n$fileName: \n";
            $majorJson = json_decode(file_get_contents($dir.$fileName));
            $majorList = $majorJson->resultList;
            foreach ($majorList as $major){
                //专业目录
                if(empty(KMajorCatalog::findOne([['ZYDM' => $major->zybm]]))) {
                    $majorCatalog =  new KMajorCatalog();
                    $majorCatalog->ZYDM = $major->zybm;
                    $majorCatalog->ZYMC = $major->zydlmc;
                    $majorCatalog->MLBB = $major->mlmc;
                    $majorCatalog->save();
                }

                $school = School::findOne([
                    'ZWMC' => $major->xxjgmc
                ]);

                if(empty($school))
                {
                    echo $major->xxjgmc,PHP_EOL;
                    continue;
                }

                $model = Major::findOne([
                    'XXID' => $school->ID,
                    'ZYID' => $major->id,
                ]);

                if(empty($model))
                {
                    $model = new Major();
                    $model->XXID =$school->ID;
                }

                $model->ZYID = $major->id;
                //专业名称
                $model->ZYMC = empty($major->zymc)?null:$major->zymc;
                //专业代码
                $model->ZYDM = empty($major->zybm)?null:$major->zybm;
                //专业简称
                $model->ZYJC = empty($major->zyjc)?null:$major->zyjc;
                //本校专业编号
                $model->BXZYBH = empty($major->zybm)?null:$major->zybm;
                //培养方案
                $model->PYFA = null;
                //开设时间
                $model->KSSJ = null;
                //专业目录版本
                $model->ZYMLBB = empty($major->mlmc)?null:$major->mlmc;
                //专业类别
                $model->ZYLB = empty($major->zydlmc)?null:$major->zydlmc;
                //专业方向
                $model->ZYFX = empty($major->zyfxmc)?null:$major->zyfxmc;
                //招生类别
                $model->ZSLB = null;
                //学制
                $model->XZ = empty($major->xz)?null:$major->xz;
                //教学形式
                $model->JXXS = null;
                //总学分
                $model->ZXF = null;
                //必修学分
                $model->BXXF = null;
                //是否国控
                $model->SFGK = empty($major->sfts)?null:$major->sfts;
                //是否涉农专业
                $model->SFSN =  empty($major->sfsn)?null:$major->sfsn;
                //招生资格
                $model->ZSZG = null;
                //状态
                $model->ZT =  $tag == "zc"? "正常":($tag == "sh"? "待审核":"停招");

                if($model->save())
                    echo ".";
                else
                {
                    var_dump($model);
                    break;
                }

            }

        }
    }
}